package com.sunni.dao_old.impl;

import com.sunni.dao_old.RoleDao;
import com.sunni.entity.Role;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

@Component("roleDao")
public class RoleDaoImpl implements RoleDao {

    @Autowired
    @Qualifier(value = "jdbcTemplate")
    private JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    /**
     * day06-11 : 角色页面展示
     * @return roleList
     */
    @Override
    public List<Role> selectAll() {
        return jdbcTemplate.query("select * from sys_role", new BeanPropertyRowMapper<Role>(Role.class));
    }

    /**
     * day06-12 : 添加角色
     */
    @Override
    public void add(Role role) {
        jdbcTemplate.update("insert into sys_role values(?,?,?)",null,role.getRoleName(),role.getRoleDesc());
    }

    /**
     * day06-14 : 根据userId ，查询对应的角色
     * @param userId user的id
     * @return roleList 所有角色，是所有的 所以是roleList
     */
    @Override
    public List<Role> selectByUserId(Long userId) {
        return jdbcTemplate.query("SELECT * FROM sys_role WHERE id IN(SELECT roleId FROM sys_user_role WHERE userId = ?)",
                new BeanPropertyRowMapper<Role>(Role.class),userId);
    }
}
